import requests
import json

class ReqQQ:

    def __init__(self,user_name:str):
        self.user_name = user_name
        self.cookies = ''
        self.pwd = '123456'
        self.req_ip = "39.101.184.255:1314"
        self.user = None
        print("init success!")
        self.login()
    
    def request_sec(self,url, param):
        req_url = "http://"+self.req_ip+""+url
        res = requests.post(url=req_url,data=param)
        return res

    def login(self):
        params = {'phone':self.user_name,'pwd':self.pwd}
        ret = self.request_sec("/request/user/loginUser", params)
        res_json = ret.json()
        self.cookies = res_json['result']['cookies']
        self.user = res_json
        return ret.json()
    
    def get_opt_info(self,opt_id:str):
        params = {'optId':opt_id}
        ret = self.request_sec("/request/tradinfo/getTradePrice",params)
        return ret.json()
        
    def buy(self,tickerSymbol:str,price:float,vol:int,dir:int):
        params = {
            'cookies':self.cookies,
            'tickerSymbol':tickerSymbol,
            'dir':dir,
            'entrustMoney':price,
            'entrustNo':vol,
            'type':0,
            'imei':'pc',
            'ip':'0.0.0.1',
            'tip':'pc'
            }
        ret = self.request_sec("/request/entrust/buyEntrust",params)
        return ret.json()

if __name__ == '__main__':
    rq = ReqQQ("13100000004")
    rq.login